// pages/my/vip/vip_invitation/index.js
const Controller = require('../../../controllers/ControllerMy').controller;


Page({

  /**
   * 页面的初始数据
   */
  data: {
    invitationList:[],
    pageNumber:1,
    lastPage:false,
    showDialog:false,
    shareData:{},
    num:''
  },

  onLoad: function (options) {
    this.getInvitationList(true)
  },
  onShow: function () {

  },
  onReachBottom(){
    if(this.data.lastPage)return
    this.getInvitationList()
  },
  getInvitationList(isFirst = false){
    if(isFirst){
      this.setData({
        pageNumber:1,
        invitationList:[]
      })
    }
    let pageNumber=this.data.pageNumber
    let invitationList=this.data.invitationList
    let value={
      shopId:wx.getStorageSync('shopId'),
      sessionId:wx.getStorageSync('usertoken'),
      pageNumber,
      pageSize:20
    }
    pageNumber++
    Controller.invitationPage(value).then(res=>{
      invitationList=invitationList.concat(res.data.list)
      this.setData({
        lastPage:res.data.lastPage,
        invitationList,
        pageNumber
      })
    })
   
  },
  delete(e){
    const that = this
    const invitationAccountId = e.currentTarget.dataset.invitationaccountid
    let value={
      shopId:wx.getStorageSync('shopId'),
      sessionId:wx.getStorageSync('usertoken'),
      invitationAccountId
    }
    wx.showModal({
      title: '确认删除',
      success(res){
        if(res.confirm){
          Controller.invitationDelete(value).then(res=>{
            wx.showToast({
              title: '删除成功',
            })
            that.getInvitationList(true)
          })
        }
      }
    })
    
  },
  toInvitationDetail(e){
    const invitationId = e.currentTarget.dataset.invitationid
    wx.redirectTo({
      url: '/pages/my/vip/vip_invitation/vip_invitation_detail/index?invitationId='+invitationId,
    })
  },
  toMyIndex(){
    wx.reLaunch({
      url: '/pages/my/my-index/my-index'
    })
  },
  go(){
    wx.navigateTo({
      url: '/pages/my/vip/vip_invitation/vip_invitation_share/index?invitationId='+26+'&invitationAccountId='+34+'&num='+5,
    })
  },
  openDialog(e){
    this.setData({
      showDialog:true,
      shareData:e.target.dataset.item
    })
  },
  closeDialog(){
    this.setData({
      showDialog:false,
      num:''
    })
  },
  inputNum(e) {
    let num = e.detail.value
    const shareNum= this.data.shareData.num
    if(num==''||(num>0&&num<=shareNum)){
      this.setData({
        num
      })
    }else{
      wx.showToast({
        title: '请正确填写分享数',
        icon: 'none',
        duration: 1500
      });
      this.setData({
        num:1
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    this.setData({
      showDialog:false,
    })

    let num = this.data.num?this.data.num:1
    const shareData = e.target.dataset.item || this.data.shareData

    let invitationId= shareData.invitationId
    let invitationAccountId= shareData.id
    let title = shareData.invitationNick+'的邀请函'
    console.log(invitationId,invitationAccountId,title);
    // 私密转发
    // wx.updateShareMenu({
    //   withShareTicket: true,
    //   isPrivateMessage: true,
    //   activityId: invitationAccountId,
    // })
    return {
      title,
      path: '/pages/my/vip/vip_invitation/vip_invitation_share/index?invitationId='+invitationId+'&invitationAccountId='+invitationAccountId+'&num='+num,
      imageUrl:"/assets/my/img/invitation_4.jpg",
    }
  }
})